/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.basedata.category.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nmf.dao.gen.bean.BaseCategory;
import com.nmf.dao.gen.bean.BizCompany;
import com.nmf.site.basedata.category.enums.CategoryType;
import com.nmf.site.basedata.category.service.BaseBizCategoryService;
import com.nmf.site.basedata.category.service.BaseCategoryService;
import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.site.common.web.BaseController;

/**
 * 基础分类Controller
 * @author 北冥
 * @version 2018-12-28
 */
@Controller
@RequestMapping(value = "${adminPath}/category/baseCategory")
public class BaseCategoryController extends BaseController {

	@Autowired
	private BaseCategoryService baseCategoryService;
	@Autowired
	BaseBizCategoryService baseBizCategoryService;
	@ModelAttribute
	public BaseCategory get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BaseCategory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = baseCategoryService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BaseCategory();
		}
		return entity;
	}
	
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeView(Integer categoryTypeVal, HttpServletRequest request, RedirectAttributes redirectAttributes, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		
		CategoryType categoryType =	CategoryType.getStatus(categoryTypeVal);
		List<BaseCategory> blist = baseBizCategoryService.queryCategory(categoryType);
		for (int i=0; i<blist.size(); i++){
			BaseCategory e = blist.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			if(e.getParentCategory()!=null) {
				map.put("pId", e.getParentCategory().getId());
			}
			map.put("name", e.getCategoryName());
			mapList.add(map);
		}
		return mapList;
	}
	
	@RequiresPermissions("category:baseCategory:view")
	@RequestMapping(value = {"list", ""})
	public String list(BaseCategory baseCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BaseCategory> page = baseCategoryService.findPage(new Page<BaseCategory>(request, response), baseCategory); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "basedata/category/baseCategoryList";
		}
		return "basedata/category/baseCategoryList";
	}
	
	@RequiresPermissions("category:baseCategory:view")
	@RequestMapping(value = "form")
	public String form(BaseCategory baseCategory, Model model) {
		model.addAttribute("baseCategory", baseCategory);
		CategoryType categoryType = CategoryType.getStatus(baseCategory.getCategoryType());
		if(CategoryType.company.equals(categoryType)) {
			return "basedata/category/compCategoryForm";
		}
		return "basedata/category/collegeCategoryForm";
	}

	@RequiresPermissions("category:baseCategory:edit")
	@RequestMapping(value = "save")
	public String save(BaseCategory baseCategory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, baseCategory)){
			return form(baseCategory, model);
		}
		try{
			baseCategoryService.save(baseCategory);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/category/baseCategory/?repage";
		}
		addMessage(redirectAttributes, "保存基础分类成功");
		return "redirect:"+Global.getAdminPath()+"/category/baseCategory/?repage";
	}
	
	@RequiresPermissions("category:baseCategory:edit")
	@RequestMapping(value = "delete")
	public String delete(BaseCategory baseCategory, RedirectAttributes redirectAttributes) {
		try{
			baseCategoryService.delete(baseCategory);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/category/baseCategory/?repage";
		}
		addMessage(redirectAttributes, "删除基础分类成功");
		return "redirect:"+Global.getAdminPath()+"/category/baseCategory/?repage";
	}

}